<?php
/**
 * Description of Subcategory
 *
 * @author Admin
 */
class Category_Model_Subcategory extends WOW_Model_Acl_Abstract
{
    public function getResourceId()
    {
        return 'Subcategory';
    }

    public function getSubcategoryById($id)
    {
        $id = (int) $id;
        return $this->getResource('Subcategory')->getSubcategoryById($id);
    }

    public function getSubcategoriesByParentId($id)
    {
        return $this->getResource('Subcategory')->getSubcategoriesByParentId($ident);
    }

    public function getSubcategoriesByParentIdent($ident)
    {
         return $this->getResource('Subcategory')->getSubcategoriesByParentIdent($ident);
    }

    public function getSubcategoryByIdent($ident)
    {
        return $this->getResource('Subcategory')->getSubcategoryByIdent($ident);
    }

    public function getParentCategory($subcatid=null,$subcat_ident=null)
    {
        return $this->getResource('Subcategory')->getParentCategory($subcatid, $subcat_ident);
    }

    public function getSubcategoryArrayByParentId($catid)
    {
        $allcat  = $this->getResource('Subcategory')->getSubcategoriesByParentId($catid);
        $ret_ar = array(' ' => 'Select Subcategory');
        foreach($allcat as $cat)
        {
            $ret_ar[$cat->subcatid] = $cat->SubCategoryName;
        }
        return $ret_ar;
    }

    public function getAllSubcategoriesArray()
    {
        $allcat  = $this->getResource('Subcategory')->getAllSubcategories();
        $ret_ar = array(' ' => 'Select Subcategory');
        foreach($allcat as $cat)
        {
            $ret_ar[$cat->subcatid] = $cat->SubCategoryName;
        }

        return $ret_ar;
    }

    public function getAllSubcategories()
    {
         return $this->getResource('Subcategory')->getAllSubcategories();
    }

    public function deleteSubcategory($subcat)
    {
        if ($subcat instanceof Category_Resource_Subcategory_Item_Interface) {
            $subcatid = (int) $subcat->subcatid;
        } else {
           $subcatid = (int) $subcat;
        }

        $subcat = $this->getSubcategoryById($subcatid);

        if (null !== $subcat) {
            $subcat->delete();
            return true;
        }
        else
             throw new WOW_Exception('Unknown Sub-category');

        return false;
    }

    public function saveSubcategory($post)
    {
        $form = $this->getForm('SubcategorySubcategoryform');
        return $this->_save($form, $post);
    }

    protected function _save(Zend_Form $form, array $info, $defaults=array())
    {
       if (!$form->isValid($info))
            return false;

        $data = $form->getValues();
        foreach ($defaults as $col => $value)
        {
            $data[$col] = $value;
        }
        // echo '<br> <pre>'; print_r($data); echo '</pre>';die();
        //find parent category Ident for subcat_ident field
        $model =  new Category_Model_Category();
        $parent_cat = $model->getCategoryById($data['catid']);
        $data['SubCategoryName'] = ucwords($data['SubCategoryName']);
        $ident = $data['SubCategoryName'];
        $data['subcat_ident']   = $parent_cat->catident. '_'. str_replace(' ', '-', strtolower($ident));

        if($data['subcatid'] !='')
        {   
            $data['updated_at'] = date('Y-m-d H:i:s');
        }
        else
        {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
        }
        
        $subcat = ($data['subcatid'] !='') ? $this->getResource('Subcategory')->getSubcategoryById($data['subcatid']) : null;

        return $this->getResource('Subcategory')->saveRow($data, $subcat);
    }

    #@ TODO:: needs to be worked on ACL later
    public function setAcl(WOW_Acl_Interface $acl)
    {
        if (!$acl->has($this->getResourceId()))
        {
            $acl->add($this)->allow('Normal', $this, array('register'))
            ->allow('Moderator', $this, array('saveCategory'))
                    ->allow('Admin', $this);
        }
        $this->_acl = $acl;
        return $this;
    }

    public function getAcl()
    {
        if (null === $this->_acl) {
            $this->setAcl(new Main_Model_Acl_Main());
        }
        return $this->_acl;
    }
    
}
?>
